package ass.dao.support;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import ass.beans.MovieBean;
import ass.beans.UserBean;
import ass.beans.UserFriendsBean;
import ass.beans.UserRecommendMovieBean;
import ass.common.DBConnectionFactory;
import ass.common.ServiceLocatorException;
import ass.dao.DataAccessException;
import ass.dao.UserRecommendMovieDAO;

public class UserRecommendMovieDAOImpl implements UserRecommendMovieDAO {
	/**
	 * The service locator to retrieve database connections from
	 */
	
	private DBConnectionFactory services;
	
	/** Creates a new instance of UserRecommendMovieDAOImpl */
	public UserRecommendMovieDAOImpl() {
		try {
			services = new DBConnectionFactory();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
		}
	}

	public UserRecommendMovieDAOImpl(DBConnectionFactory services) {
		this.services = services;
	}

	public void insert(UserRecommendMovieBean bean) throws DataAccessException {
		Connection con = null;
		   try { 
		 con = services.createConnection();
	     PreparedStatement stmt = con.prepareStatement(
	       "INSERT INTO TBL_USER_RECOMMENDED_MOVIE (USERNAME, MOVIE_ID) values (?, ?)");
	     stmt.setString(1, bean.getUsername());
	     stmt.setInt(2, bean.getMovie_id());

	     
	     int n = stmt.executeUpdate();

	     stmt.close();
	     if (n != 1)
	       throw new DataAccessException("Did not insert one row into database");
	   } catch (ServiceLocatorException e) {
	       throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
	   } catch (SQLException e) {
	       throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
	   } finally {
	      if (con != null) {
	         try {
	           con.close();
	         } catch (SQLException e1) {
	           e1.printStackTrace();
	         }
	      }
	   }
	}
	
	public List<MovieBean> getRecommendedMovieList(String username) {
		List<MovieBean> list = new ArrayList<MovieBean>();
		Connection con = null;
		try {
			con = services.createConnection();
			// convert('2003-07-08',DATE)
			PreparedStatement stmt = con
					.prepareStatement("select distinct(tm.*) from TBL_USER_RECOMMENDED_MOVIE tr, "+
										 "TBL_MOVIES tm, "+
										 "TBL_USER_FRIENDS tf "+
										"where tr.MOVIE_ID = tm.MOVIE_ID "+
										"and (tf.USERNAME = tr.USERNAME "+
										"or tf.USERNAME_FRIEND = tr.USERNAME) "+
										"and tf.IS_ACCEPTED = true "+
										"and tr.USERNAME != ?" +
										"order by tm.movie_rating desc");
			stmt.setString(1, username);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				MovieBean movieBean = new MovieDAOImpl().createMovieBean(rs);
				list.add(movieBean);
			}
			stmt.close();
		} catch (ServiceLocatorException e) {
			throw new DataAccessException("Unable to retrieve connection; "
					+ e.getMessage(), e);
		} catch (SQLException e) {
			throw new DataAccessException("Unable to execute query; "
					+ e.getMessage(), e);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}

		return list;
	}
	
	
	public List<UserBean> getRecommenderList(String username, int movie_id) {
		List<UserBean> list = new ArrayList<UserBean>();
		Connection con = null;
		try {
			con = services.createConnection();
			// convert('2003-07-08',DATE)
			PreparedStatement stmt = con
					.prepareStatement("select distinct(tu.*) " +
									" from TBL_USER_RECOMMENDED_MOVIE tr, "+
										 " TBL_USERS tu, " +
										 " TBL_USER_FRIENDS tf " +
									" where tr.USERNAME = tu.USERNAME" +
									" and (tf.username = ?" +
									" or tf.username_friend = ?)" +
									" and (tf.username = tu.username" +
									" or tf.username_friend = tu.username)" +
									" and tf.is_accepted = true"+
									" and tr.movie_id = ?  " +
									" and tr.USERNAME != ?");
			stmt.setString(1, username);
			stmt.setString(2, username);
			stmt.setInt(3, movie_id);
			stmt.setString(4, username);
			ResultSet rs = stmt.executeQuery();
			
			while (rs.next()) {
				UserBean userBean = new UserDAOImpl().createUserBean(rs);
				list.add(userBean);
			}
			stmt.close();
		} catch (ServiceLocatorException e) {
			throw new DataAccessException("Unable to retrieve connection; "
					+ e.getMessage(), e);
		} catch (SQLException e) {
			throw new DataAccessException("Unable to execute query; "
					+ e.getMessage(), e);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}

		return list;
	}
	
	private UserRecommendMovieBean createUserRecommendMovieBean(ResultSet rs) throws SQLException {
		UserRecommendMovieBean userRecommendMovieBean = new UserRecommendMovieBean();
		userRecommendMovieBean.setUsername(rs.getString("USERNAME"));
		userRecommendMovieBean.setMovie_id(rs.getInt("MOVIE_ID"));
	return userRecommendMovieBean;
	}
	
}
